//
//  LZCollectionView.m
//  collectionViewDemo
//
//  Created by langZai on 2017/3/1.
//  Copyright © 2017年 langZai. All rights reserved.
//

#import "LZCollectionView.h"
#import "LZRecommendCell.h"

static NSString * cellID = @"lz_collectionViewCell";
@interface LZCollectionView ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property(nonatomic, strong)UICollectionView *mainCollectionView;
@end

@implementation LZCollectionView
- (instancetype)init {
    if (self = [super init]) {
        [self addSubview:self.mainCollectionView];
    }
    return self;
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self addSubview:self.mainCollectionView];
        [self.mainCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
    }
    return self;
}
- (void)setRArray:(NSMutableArray *)rArray {
    _rArray = rArray;
    [_mainCollectionView reloadData];
}

//lazy
-(UICollectionView *)mainCollectionView {
    if (_mainCollectionView) {
        return _mainCollectionView;
    }
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(165, 120);
    layout.minimumLineSpacing = 5;
    layout.minimumInteritemSpacing = 5;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.sectionInset = UIEdgeInsetsMake(5, 1, 5, 5);
    
    _mainCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    _mainCollectionView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    _mainCollectionView.delegate = self;
    _mainCollectionView.dataSource = self;
    _mainCollectionView.scrollsToTop = NO;
    _mainCollectionView.showsVerticalScrollIndicator = NO;
    _mainCollectionView.showsHorizontalScrollIndicator = NO;
    _mainCollectionView.bounces = NO;
    [_mainCollectionView registerClass:[LZRecommendCell class] forCellWithReuseIdentifier:cellID];
    return _mainCollectionView;
}

#pragma mark - UICollectionViewDelegate,UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _rArray? _rArray.count:0;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    LZRecommendCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
//    cell.backgroundColor = [UIColor yellowColor];
    if (_rArray&&_rArray.count != 0) {
        ZRecommendModel * model = _rArray[indexPath.row];
        [cell.myImageView sd_setImageWithURL:[NSURL URLWithString:model.cover] placeholderImage:[UIImage new]];
        cell.mtitleLabel.text = model.title;
    }
    
    cell.myImageView.backgroundColor = [UIColor whiteColor];
    

    return cell;
}
// 选中哪一行哪一个item
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (self.lzcDelegate && [self.lzcDelegate conformsToProtocol:@protocol(LZCollectionViewDelegate)]) {
        [self.lzcDelegate  didSeletWithRow:indexPath.row withVid:_rArray[indexPath.row].vid];
    }
}





@end


